package com.kun.service;

import com.kun.entity.Menu;
import com.kun.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public int addMenu(Menu menu) {
        if (menu.getId()!=null){
            return menuMapper.updateMenu(menu);
        }else{
            return menuMapper.addMenu(menu);
        }

    }

    @Override
    public int deleteMenu(int id) {
        return menuMapper.deleteMenu(id);
    }

    @Override
    public int updateMenu(Menu menu,Map map) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public List<Menu> selectAllMenus(Map map) {
        if (map.containsKey("pageIndex") && map.containsKey("pageSize")){
            Integer pageIndex = (Integer) map.get("pageIndex");
            Integer pageSize= (Integer) map.get("pageSize");
            pageIndex=pageSize*(pageIndex-1);
            map.replace("pageIndex",pageIndex);
        }
        return menuMapper.selectAllMenus(map);
    }
}
